<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="openDialog(1,0)">添加权限</el-button>
      <el-table border :data="permissionList" row-key="id">
        <el-table-column label="名称" prop="name" />
        <!-- 这里的标识都是唯一的，后面需要用的 -->
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type===1" size="mini" type="text" @click="openDialog(2,row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="hanldr(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <dianlogvue ref="dianlogRef" :is-show.sync="isShow" :type="type" :pid="pid" @update-list="loadList" />
    </div>
  </div>
</template>
<script>
import { delPermission, getPermission } from '@/api/permisson'
import { listTree } from '@/utils'
import dianlogvue from './components/dialog-index.vue'
export default {
  name: 'Permission',
  components: {
    dianlogvue
  },
  data() {
    return {
      permissionList: [],
      isShow: false,
      type: null,
      pid: 0
    }
  },
  mounted() {
    this.loadList()
  },
  methods: {
    async loadList() {
      const res = await getPermission()
      this.permissionList = await listTree(res, 0)
    },
    async del(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.loadList()
    },
    openDialog(type, pid) {
      this.isShow = true
      this.type = type
      this.pid = pid
    },
    async hanldr(id) {
      await this.$refs.dianlogRef.getUserPer(id)
      this.isShow = true
    }
  }
}
</script>

<style scoped lang="scss">
.btn-add {
  margin: 10px;
}
.app-container {
  padding: 20px;
}
</style>
